cmake_minimum_required(VERSION 3.0)

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_STANDARD 17)


# abls----------------start
# if(CMAKE_SYSTEM_NAME MATCHES "Windows")
# message(STATUS "Now is windows")
# if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
#   set(ABSL_HOME ${PROJECT_SOURCE_DIR}/src/third_party/abseil/lib/win/debug)
# else()
#   set(ABSL_HOME ${PROJECT_SOURCE_DIR}/src/third_party/abseil/lib/win/release)
# endif()
# elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
# message(STATUS "Now is UNIX-like OS's.")
# set(ABSL_HOME ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/abseil/lib/unix)
# endif()

# # Abseil lib
# set(AbslLibs
#   ${ABSL_HOME}/libabsl_base.so
#   ${ABSL_HOME}/libabsl_int128.so
#   ${ABSL_HOME}/libabsl_city.so
#   ${ABSL_HOME}/libabsl_hash.so
#   ${ABSL_HOME}/libabsl_hashtablez_sampler.so
#   ${ABSL_HOME}/libabsl_random_seed_gen_exception.so
#   ${ABSL_HOME}/libabsl_random_seed_sequences.so
#   ${ABSL_HOME}/libabsl_raw_hash_set.so
#   ${ABSL_HOME}/libabsl_malloc_internal.so
#   ${ABSL_HOME}/libabsl_spinlock_wait.so
#   ${ABSL_HOME}/libabsl_synchronization.so
#   ${ABSL_HOME}/libabsl_throw_delegate.so
#   ${ABSL_HOME}/libabsl_raw_logging_internal.so
#   ${ABSL_HOME}/libabsl_time.so
#   ${ABSL_HOME}/libabsl_time_zone.so)
  
# abls----------------end

# add include and lib dirs
include_directories(${PROJECT_SOURCE_DIR}/src/third_party/abseil/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/stdBase/include)
include_directories(${PROJECT_SOURCE_DIR}/src/third_party)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/stdBase/graph)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/log)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/string)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/tcl)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

find_package(TCL 8.6 QUIET)
if(TCL_FOUND)
  include_directories(${TCL_INCLUDE_PATH})
  link_directories(${TCL_LIBRARY})
else(TCL_FOUND)
  # install Tcl 8.6 https://github.com/tcltk/tcl/tree/core-8-6-branch
  message(FATAL_ERROR "Could not find TCL!")
endif(TCL_FOUND)

link_directories(${CMAKE_BINARY_DIR}/lib)
link_directories(${PROJECT_SOURCE_DIR}/src/third_party/glog)
link_directories(${PROJECT_SOURCE_DIR}/src/third_party/gflags)
link_directories(${ABSL_HOME})

add_subdirectory(log)
add_subdirectory(string)
add_subdirectory(tcl)
add_subdirectory(time)
add_subdirectory(stdBase)
add_subdirectory(usage)
# add_subdirectory(test)


option(BASE_RUN_TESTS "If ON, the tests will be run." ON)

# if(BASE_RUN_TESTS AND BUILD_PR_FLOW)
  message(STATUS "RUN BASE TESTS")
  # declare googletest
  if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
      include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/win/debug)
      link_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/win/debug)
    elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
      include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/unix)
      link_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/unix)
    endif()

  else()
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
      include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/win/release)
      link_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/win/release)
    elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
      include_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/unix)
      link_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/googletest/unix)
    endif()

  endif()

  # build test
  set(SourceFiles
      # ./test/AdjListGraphTest.cc
      # ./test/ArrayCompareTest.cc
      # ./test/ArrayCompareTestSTL.cc
      # ./test/ArrayListTest.cc
      # ./test/ArrayTest.cc
      # ./test/CompareTest.cc
      # ./test/HashMapTest.cc
      # ./test/HashSetTest.cc
      # ./test/ListTest.cc
      # ./test/LogTest.cc
      # ./test/MapTest.cc
      # ./test/MemoryPoolTest.cc
      # ./test/ScriptEngineTest.cc
      # ./test/SetTest.cc
      # ./test/StrTest.cc
      # ./test/TimeTest.cc
      # ./test/VectorCompareTest.cc
      # ./test/VectorCompareTestSTL.cc gg
      ./test/VectorTest.cc)
  add_executable(base_test ${SourceFiles})

  set(MyLibs
      log
      tcl
      str
      time
      graph
      glog
      gflags
      absl_time
      unwind)

  file(GLOB SO_FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/abseil/lib/unix/*.so)

  file(GLOB GLOG_SO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/glog/lib*)

  if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
      target_link_libraries(base_test gmock_maind gtestd gmockd ${AbslLibs}
                            ${MyLibs})
    elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
      target_link_libraries(
        base_test
        gmock_main
        gtest
        gmock
        pthread
        ${AbslLibs}
        ${MyLibs})
      add_custom_command(
        TARGET base_test
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SO_FILES}
                ${CMAKE_CURRENT_BINARY_DIR}/
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GLOG_SO_FILES}
                ${CMAKE_CURRENT_BINARY_DIR}/)
    endif()
  else()
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
      target_link_libraries(base_test gmock_main gtest gmock ${AbslLibs})
    elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
      target_link_libraries(
        base_test
        gmock_main
        gtest
        gmock
        pthread
        ${AbslLibs}
        ${MyLibs})
      add_custom_command(
        TARGET base_test
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SO_FILES}
                ${CMAKE_CURRENT_BINARY_DIR}/
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GLOG_SO_FILES}
                ${CMAKE_CURRENT_BINARY_DIR}/)
    endif()
  endif()

# endif(BASE_RUN_TESTS AND BUILD_PR_FLOW)
